package com.ruoyi.common.utils;


import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @ClassName CommonUtils
 * @Description TODO
 * @Author wangdongliang
 * @Date 2022/8/13 15:40
 */
public class CommonUtils {

    /**
     * 获取用户id
     * @param request
     * @return
     */
    public static Long getUserId(HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        return TokenUtils.getToken(token);
        //return 1L;
    }

    public static Long getGoodsInfoUserId(HttpServletRequest request){
        try {
            String token = request.getHeader("Authorization");
            return TokenUtils.getToken(token);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取uuid4
     * @return
     */
    public static String get4UUID(){
        UUID id=UUID.randomUUID();
        String[] idd=id.toString().split("-");
        return idd[0]+"-"+idd[1]+"-"+idd[2]+"-"+idd[3]+"-"+idd[4];
    }

    /**
     * 获取活动编号
     * @return
     */
    public static String getActivityNo(Long id){
        int requiredLength = 6; // 需要补足的位数
        int currentLength = String.valueOf(id).length();
        int missingLength = requiredLength - currentLength;

        if (missingLength > 0) {
            String format = "%0" + missingLength + "d";
            String result = String.format(format, id);
            return "88"+result;
        }
        return "88"+id;
    }
}
